﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace belov_test
{
    public partial class CreateAdminForm : Form
    {
        private const string BANNED_SYMBOLS = " !@#$%^&*()?/,.";

        private AdminInfo m_newAdmin;

        public CreateAdminForm()
        {
            InitializeComponent();
        }

        public AdminInfo NewAdmin
        {
            get { return m_newAdmin; }
            set { m_newAdmin = value; }
        }

        private void OnCreateNewAdmin(object sender, EventArgs e)
        {
            m_newAdmin = TryCreateAdmin();
            if (m_newAdmin != null)
            {
                Close();
            }
        }

        private void OnCancel(object sender, EventArgs e)
        {
            Close();
        }

        private AdminInfo TryCreateAdmin()
        {
            String login = m_loginTextBox.Text.Trim();
            String password = m_passwordTextBox.Text.Trim();

            for (int i = 0; i < login.Length; i++)
            {
            	if (BANNED_SYMBOLS.Contains(login[i].ToString()))
            	{
                    MessageBox.Show("Логин не должен содержать символов " + BANNED_SYMBOLS + "!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
            	}
            }

            if (String.IsNullOrEmpty(login))
            {
                MessageBox.Show("Введите логин!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return null;
            }

            if (String.IsNullOrEmpty(password))
            {
                MessageBox.Show("Введите пароль!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return null;
            }
            AdminInfo info = new AdminInfo();
            info.Login = login;
            info.Password = password;

            return info;
        }

        private void OnChecked(object sender, EventArgs e)
        {
            m_passwordTextBox.UseSystemPasswordChar = !m_showPasswordsCB.Checked;
        }
    }
}
